﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using gfVehicleLibraryAJ;
using ConvertInfo;
using ProcessManage;
using AJNetLib;
using Newtonsoft.Json;

namespace AJNetDll_XZSN
{
    public class QueryVehicle
    {
        public ReturnInfo QueryVehicleInfo(QueryCondition queryCondition)
        {
            
            ReturnInfo retInfo = new ReturnInfo();
            ReturnInfo retInfo18S01 = new ReturnInfo();
            try
            {
                Vehicle_18C49_Para m_18C49_Para = Get_18C49_FromDic(queryCondition.DicQueryPara);
                m_18C49_Para.jkid = "18C49";
                Vehicle_18C49_Result m_Result_18C49 = Query_18C49(m_18C49_Para);
                retInfo.IsSucessful = m_Result_18C49.code == "1" ? true : false;
                if (retInfo.IsSucessful && m_Result_18C49.hphm != null)
                {
                    Dictionary<string, string> vehicleInfo = new Dictionary<string, string>();
                    Dictionary<string, string> result = new Dictionary<string, string>();
                    vehicleInfo.Add("HPHM", m_Result_18C49.hphm);
                    vehicleInfo.Add("HPZLDH", m_Result_18C49.hpzl);
                    vehicleInfo.Add("VIN", m_Result_18C49.clsbdh);
                    vehicleInfo.Add("CLZLDH", m_Result_18C49.cllx);
                    vehicleInfo.Add("SYR", m_Result_18C49.syr);
                    vehicleInfo.Add("SYXZDH", m_Result_18C49.syxz);
                    vehicleInfo.Add("PP", m_Result_18C49.clpp1);
                    vehicleInfo.Add("XH", m_Result_18C49.clxh);
                    vehicleInfo.Add("FDJH", m_Result_18C49.fdjh);
                    vehicleInfo.Add("FDJZZCS", m_Result_18C49.zzcmc);
                    vehicleInfo.Add("FDJXH", m_Result_18C49.fdjxh);
                    vehicleInfo.Add("YZLJ", m_Result_18C49.qlj.ToString());
                    vehicleInfo.Add("CCDJRQ", m_Result_18C49.ccdjrq.ToString("yyyy-MM-dd"));
                    vehicleInfo.Add("CCRQ", m_Result_18C49.ccrq.ToString("yyyy-MM-dd"));
                    vehicleInfo.Add("CYS", m_Result_18C49.hdzk.ToString());
                    vehicleInfo.Add("ZZL", m_Result_18C49.zzl.ToString());
                    vehicleInfo.Add("ZBZL", m_Result_18C49.zbzl.ToString());
                    vehicleInfo.Add("HDZH", m_Result_18C49.hdzzl.ToString());
                    vehicleInfo.Add("CSC", m_Result_18C49.cwkc.ToString());
                    vehicleInfo.Add("CSG", m_Result_18C49.cwkg.ToString());
                    vehicleInfo.Add("CSK", m_Result_18C49.cwkk.ToString());
                    vehicleInfo.Add("CSYSDH", m_Result_18C49.csys.ToString());
                    vehicleInfo.Add("RLLBDH", m_Result_18C49.rlzl.ToString());
                    vehicleInfo.Add("CLYTDH", m_Result_18C49.clyt.ToString());
                    vehicleInfo.Add("YTSXDH", m_Result_18C49.ytsx.ToString());
                    vehicleInfo.Add("ZZS", m_Result_18C49.zs.ToString());
                    if (!string.IsNullOrEmpty(m_Result_18C49.zxxs))
                    { vehicleInfo.Add("ZXZLXDH", m_Result_18C49.zxxs); }
                    vehicleInfo.Add("EDGL", m_Result_18C49.gl.ToString());
                    vehicleInfo.Add("FDJPL", m_Result_18C49.pl.ToString());
                    vehicleInfo.Add("CPMC", m_Result_18C49.clpp1.ToString());
                    vehicleInfo.Add("HXNBCD", m_Result_18C49.hxnbcd.ToString());
                    vehicleInfo.Add("HXNBKD", m_Result_18C49.hxnbkd.ToString());
                    vehicleInfo.Add("HXNBGD", m_Result_18C49.hxnbgd.ToString());
                    vehicleInfo.Add("SFMJ", m_Result_18C49.sfmj.ToString());
                    vehicleInfo.Add("QLJ", m_Result_18C49.qlj.ToString());                    
                    if (!string.IsNullOrEmpty(m_Result_18C49.bmjyy))
                    {
                        vehicleInfo.Add("BMJYY", m_Result_18C49.bmjyy.ToString());
                    }
                    vehicleInfo.Add("LWCXWZJL", m_Result_18C49.zt.ToString());
                    vehicleInfo.Add("ZJ", m_Result_18C49.zj.ToString());

                    string resultJson = JsonConvert.SerializeObject(vehicleInfo);
                    result.Add("vehicleInfo", resultJson);
                    retInfo.DicRet = result;
                    retInfo.Message = m_Result_18C49.message;
                    retInfo.IsSucessful = true;
                }
                else
                {
                    Dictionary<string, string> result = new Dictionary<string, string>();
                    result.Add("vehicleInfo", string.Empty);
                    retInfo.DicRet = result;
                    retInfo.Message = m_Result_18C49.message;
                    retInfo.IsSucessful = false;
                }
            }
            catch (Exception ex)
            {
                retInfo.IsSucessful = false;
                retInfo.Message = ex.Message;
            }

            return retInfo;
        }

        public ReturnInfo QueryByJKId(QueryCondition queryCondition)
        {

            ReturnInfo retInfo = new ReturnInfo();
            ReturnInfo retInfo18S01 = new ReturnInfo();
            try
            {
                string strJKID = queryCondition.DicQueryPara["jkid"];
                switch (strJKID)
                {                  
                    case "18S01":
                        Vehicle_18S01_Para m_18S01_Para = Get_18S01_FromDic(queryCondition.DicQueryPara);
                        Vehicle_18S01_Result m_Result_18S01 = Query_18S01(m_18S01_Para);
                        retInfo.IsSucessful = m_Result_18S01.code == "1" ? true : false;
                        if (retInfo.IsSucessful)
                        {
                            Dictionary<string, string> dicRet = new Dictionary<string, string>();                          
                            dicRet.Add("AJLSH", m_Result_18S01.jylsh);
                            dicRet.Add("JYCS", m_Result_18S01.jycs);
                            retInfo.DicRet = dicRet;
                        }
                        else
                        {
                            retInfo.Message = m_Result_18S01.message;
                        }
                        break;

                    case "18C49":
                        return QueryVehicleInfo(queryCondition);
                 }
            }
            catch (Exception ex)
            {
                retInfo.IsSucessful = false;
                retInfo.Message = ex.Message;
            }

            return retInfo;
        }

        private Vehicle_18C49_Result Query_18C49(Vehicle_18C49_Para para_18C49)
        {
            AJ_ReadProcess readProcess = new AJ_ReadProcess();
            Vehicle_18C49_Result result_18C49 = readProcess.AJ_Read_18C49(para_18C49);
            return result_18C49;
        }

        private Vehicle_18S01_Result Query_18S01(Vehicle_18S01_Para para_18S01)
        {
            AJ_ReadProcess readProcess = new AJ_ReadProcess();
            Vehicle_18S01_Result result_18S01 = readProcess.AJ_Read_18S01(para_18S01);
            return result_18S01;
        }

        public ReturnInfo QueryVehicleInfo(string queryParaJson)
        {
            Vehicle_18C49_Para c49para = 
                Newtonsoft.Json.JsonConvert.DeserializeObject<Vehicle_18C49_Para>(queryParaJson);
            Vehicle_18C49_Result c49result = Query_18C49(c49para);
            ReturnInfo queryResult = new ReturnInfo();
            queryResult.IsSucessful = c49result.code == "1";
            queryResult.Message = c49result.message;
            queryResult.DicRet = new Dictionary<string, string>();
            if (c49result.code == "1")
            {
                var loginVehicleInfo = new
                {
                    HPHM=c49result.hphm,
                    HPZLDH = c49result.hpzl,
                    VIN = c49result.clsbdh,
                    CLZLDH = c49result.cllx,
                    SYR = c49result.syr,
                    SYXZDH = c49result.syxz,
                    PP = c49result.clpp1,
                    XH = c49result.clxh,
                    FDJH = c49result.fdjh,
                    FDJZZCS = c49result.zzcmc,
                    FDJXH = c49result.fdjxh,
                    YZLJ = c49result.qlj,
                    CCDJRQ = c49result.ccdjrq.ToString("yyyy-MM-dd"),
                    CCRQ = c49result.ccrq.ToString("yyyy-MM-dd"),
                    CYS = c49result.hdzk,
                    ZZL = c49result.zzl,
                    ZBZL = c49result.zbzl,
                    HDZH = c49result.hdzzl,
                    CSC = c49result.cwkc,
                    CSG = c49result.cwkg,
                    CSK = c49result.cwkk,
                    CSYSDH = c49result.csys,
                    RLLBDH = c49result.rlzl,
                    CLYTDH = c49result.ytsx,
                    ZXZLXDH = c49result.zxxs,
                    EDGL = c49result.gl,
                    FDJPL = c49result.pl,
                    CPMC = c49result.clpp1,
                    HXNBCD = c49result.hxnbcd,
                    HXNBKD = c49result.hxnbkd,
                    HXNBGD = c49result.hxnbgd,
                    SFMJ = c49result.sfmj,
                    BMJYY = c49result.bmjyy,
                    QLJ = c49result.qlj,
                    LWCXWZJLDH=c49result.zt
                };
                string wagonJson = Newtonsoft.Json.JsonConvert.SerializeObject(loginVehicleInfo);
                queryResult.DicRet.Add("loginVehicleInfo", wagonJson);
            }
            else 
            {
                queryResult.DicRet.Add("loginVehicleInfo", string.Empty);
            };
            return queryResult;
        }

        #region 得到字典集合
        /// <summary>
        /// Dictionary change to 18C51 Para
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private Vehicle_18C49_Para Get_18C49_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18C49_Para m_18C49_Para = new Vehicle_18C49_Para();

            m_18C49_Para = m_ModelReflection.DictionaryToModel(m_18C49_Para, dic);

            return m_18C49_Para;
        }
        private Vehicle_18S01_Para Get_18S01_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18S01_Para m_18S01_Para = new Vehicle_18S01_Para();

            m_18S01_Para = m_ModelReflection.DictionaryToModel(m_18S01_Para, dic);

            return m_18S01_Para;
        }
        #endregion
    }
}
